package tag

import (
	"hotel_Manager_Api/model/dbEngin"
	"hotel_Manager_Api/model/dbModel"
	"github.com/martini-contrib/encoder"
	"github.com/go-martini/martini"
	"hotel_Manager_Api/util"
	"github.com/go-xorm/xorm"
)

func GetAllTags(enc encoder.Encoder)(int,[]byte,error)  {
	db:=dbEngin.GetEnging()
	tags := make([]dbModel.Tag,0)
	err :=db.Find(&tags)
	if(err!=nil){
		return 500,nil,err
	}

	return 200,encoder.Must(enc.Encode(tags)),nil
}

func NewTag(tag dbModel.Tag)(int,error)  {
	db:=dbEngin.GetEnging()
	_,err :=db.Insert(tag);
	if(err!=nil){
		return 500,err
	}
	return 201,nil
}

func EditTag(parameters martini.Params, tag dbModel.Tag,db *xorm.Engine)(int,error)  {
	id,err := util.ParamaterToInt16(parameters,"id")
	if(err!=nil){
		return 400,err
	}
	tag.Id = id
	_,err = db.Id(id).Update(tag)
	if(err!=nil){
		return 500,err
	}
	return 200,nil
}

func DeleteTag(params martini.Params,db *xorm.Engine)(int,error)  {
	id,err := util.ParamaterToInt16(params,"id")
	if(err!=nil){
		return 400,err
	}
	_,err =db.Id(id).Delete(dbModel.Tag{})
	return 200,nil
}
